'''
1.基于协议
接口代码 requests
通过分析页面找到验证点，断言 re|bs4
2.多线程
模拟真实用户使用threading.Thread

3.模拟用户场景
思考时间 time,random
拱形、门型、复杂场景 循环
4.指标收集
响应时间 时间戳  装饰器
吞吐量    psutil
资源利用率
每秒事务数
事务成功率
。。。

5.性能测试报告
'''
import json
import time
from threading import Thread,Lock

import requests
session=''
ph=15783204997
def get_session():
    global session
    session=requests.Session()

def get_time(func):
    def inner():
        start=time.time()
        resp=func()
        end=time.time()
        return round(end-start,2),resp
    return inner
@get_time
def do_login():

    string="username=admin&password=admin123&verifycode=0000"
    param=change_dict(string)
    resp=session.post("http://localhost:8080/woniusales/user/login",data=param).text
    if "login-pass"==resp:
        return "pass"
    else:
        return "do_login--------fail"
@get_time
def search_member():
    string="customerphone=15881111111&page=1"
    param=change_dict(string)
    resp=session.post("http://localhost:8080/woniusales/customer/search",data=param).text
    if "15881111111" in resp:
        return "pass"
    else:
        return "search_member-----------fail"

@get_time
def add_member():
    global ph
    with Lock():
        string=f"customername=未和&customerphone={ph}&childsex=男&childdate=2021-03-23&creditkids=0&creditcloth=0"
        param=change_dict(string)
        resp=session.post("http://localhost:8080/woniusales/customer/add",data=param).text
        ph+=1
        if "add-successful"==resp:
            return "pass"
        else:
            return "add_member----------fail"

def change_dict(string):
    string=string.replace("&",'","')
    string=string.replace("=",'":"')
    string='{"'+string+'"}'
    return  eval(string)

def json_to_dict(string):
    return  json.loads(string)

def loop(number):
    for i in range(number):
        r = do_login()
        print(r)
        r = search_member()
        print(r)
        r = add_member()
        print(r)


def add_vusers(users=0,loopnum=0):
    user_list=[]
    for i in range(users):
        t=Thread(target=loop,args=(loopnum,))
        user_list.append(t)
    for i in user_list:
        i.start()
    for i in user_list:
        i.join()
if __name__ == '__main__':
    get_session()
    add_vusers(10,20)